//着色器代码
	
const vertex_jewel_source = `#version 300 es
	precision mediump float;

	layout(location = 0) in vec3 aPos;
	layout(location = 1) in vec3 norm;
	
	uniform mat4 one_matrix;
	uniform mat4 rotate_xz;
	
	out vec3 position;//位置
	out vec3 normal;//法线

	
	
	void main()
	{
		normal = (rotate_xz * vec4(norm, 1.0)).xyz;
		position = (rotate_xz * vec4(aPos, 1.0)).xyz;

		gl_Position = one_matrix * rotate_xz * vec4(aPos, 1.0);
	}
	
	`;
	
const fragment_jewel_source = `#version 300 es
	#define NUM_VORONOI 7

	precision mediump float;

	in vec3 normal;
	in vec3 position;

	out vec4 FragColor;
	
	uniform vec3 view_position;
	uniform float seed;
	
	void main()
	{
		vec3 lightDir = vec3(0.5647, 0.7071, 0.4255);//光源方向
		float env = max(dot(lightDir, normal), 0.0);

		//voronoi噪声
		vec2 voronoi[NUM_VORONOI];
		float num_rand = seed;
		const float factor_voro_scale = 1.0;
		//随机数生成
		for(int i = 0;i < NUM_VORONOI;i += 1){
			voronoi[i].x = (num_rand - floor(num_rand) - 0.5) * factor_voro_scale;
			voronoi[i].y = (voronoi[i].x * 10.0 - floor(voronoi[i].x * 10.0) - 0.5) * factor_voro_scale;

			num_rand *= 1.5;
		}
		//距离计算
		float min_dist[2] = float[2](100.0, 100.0);

		for(int i = 0;i < NUM_VORONOI;i += 1){
			float length_tmp = length(voronoi[i] - position.xy);
			if(length_tmp < min_dist[1]){
				min_dist[1] = length_tmp;
			}

			if(min_dist[1] < min_dist[0]){
				float exchange_tmp = min_dist[0];
				min_dist[0] = min_dist[1];
				min_dist[1] = exchange_tmp;
			}
		}
		float color_voronoi = (sign(0.004 - (min_dist[1] - min_dist[0])) + 1.0) / 2.0;

		//反射效果
		const float plane_z = 3.0;//光源z轴坐标
		vec3 dir_reflect = reflect(position - view_position, normal);
		vec2 coord_reflect = dir_reflect.xy * (plane_z - position.z) / dir_reflect.z + position.xy;//反射到光源平面上的坐标
		float tmp = coord_reflect.y + 0.3 * coord_reflect.x;
		float color_reflect = (sign(tmp) + 1.0) * (sign(0.7 - tmp) + 1.0) / 4.0;

		FragColor = vec4(vec3(0.3 * env + 0.6 + color_reflect + 0.3 * color_voronoi + position.y * 0.3) * vec3(0.4, 0.9, 1.4),1.0);
	}
	
	`;
	
	
//后处理0
const vertex_after0_source = `#version 300 es
	precision mediump float;

	layout(location = 0) in vec3 aPos;
	layout(location = 1) in vec3 norm;

	out vec3 vector_normal;
	
	uniform mat4 one_matrix;
	uniform mat4 rotate_xz;

	
	void main()
	{
		vector_normal = norm;

		vec4 position_tmp = one_matrix * rotate_xz * vec4(aPos, 1.0);

		gl_Position = position_tmp;
	}
	
	`;
	
const fragment_after0_source = `#version 300 es

	precision mediump float;

	in vec3 vector_normal;

	out vec4 FragColor;

	void main()
	{

		FragColor = vec4(vector_normal, 1.0);
	}
	
	`;



//后处理1 判断边界
const vertex_rect_source = `#version 300 es
layout(location = 0) in vec4 aPos_n_texcoord;//坐标和纹理坐标

out vec2 coord_tex;
void main(){
	gl_Position = vec4(aPos_n_texcoord.xy, 0, 1);
	coord_tex = aPos_n_texcoord.zw;
}
`;
const fragment_rect_source =`#version 300 es
precision mediump float;
in vec2 coord_tex;
out vec4 FragColor;

uniform sampler2D Tex_hdr;
uniform vec2 pixel_span;

void main(){
	//边界卷积核
	const float kernel_hori[3] = float[3](1.0, 2.0, 1.0);
	vec3 hori = vec3(0.0);
	for(int i = 0;i < 3;i += 1){
		hori += texture(Tex_hdr, coord_tex + vec2(-pixel_span.x, pixel_span.y * float(i - 1))).xyz * kernel_hori[i];
	}
	for(int i = 0;i < 3;i += 1){
		hori -= texture(Tex_hdr, coord_tex + vec2(pixel_span.x, pixel_span.y * float(i - 1))).xyz * kernel_hori[i];
	}//完成水平边界卷积核

	vec3 vert = vec3(0.0);
	for(int i = 0;i < 3;i += 1){
		vert += texture(Tex_hdr, coord_tex + vec2(pixel_span.x * float(i - 1), -pixel_span.y)).xyz * kernel_hori[i];
	}
	for(int i = 0;i < 3;i += 1){
		vert -= texture(Tex_hdr, coord_tex + vec2(pixel_span.x * float(i - 1), pixel_span.y)).xyz * kernel_hori[i];
	}//完成垂直边界卷积核


	FragColor =  vec4(vec3(length(max(abs(hori), abs(vert)))), 1.0);
}
`;


const fragment_blend_source =`#version 300 es
precision mediump float;
in vec2 coord_tex;
out vec4 FragColor;

uniform sampler2D line;
uniform sampler2D jewel;

void main(){
	//门限
	float threshold = 0.01;
	float line_color = floor(texture(line, coord_tex).x - threshold + 1.0);

	FragColor =  vec4(vec3(line_color) * 0.2 + texture(jewel, coord_tex).xyz, 1.0);
}
`;